<template>
  <div class="system-configuration">
    <!-- 系统设定标题 -->
    <div class="section-header">
      <h2 class="section-title">系统设定</h2>
      <p class="section-description">
        配置系统的基础参数、算法参数和AI模型设置
      </p>
    </div>

    <!-- 算法参数设定 -->
    <div class="aps-section">
      <el-row>
        <span
          style="
            font-weight: 600;
            margin-top: 1px;
            font-size: large;
            color: rgb(1, 128, 165);
          "
        >
          算法参数设定
        </span>
        <el-button
          type="text"
          style="float: right"
          @click="toggleAlgorithmParams"
        >
          {{ showAlgorithmParams ? "收起" : "展开" }}
        </el-button>
      </el-row>
      <transition name="slide-toggle">
        <div v-show="showAlgorithmParams">
          <el-row>
            <el-form
              label-position="right"
              label-width="140px"
              style="margin-top: 30px"
            >
              <el-form-item label="排程深度">
                <el-input-number
                  v-model="algorithmParams.schedulingDepth"
                  :min="1"
                  :max="100"
                  placeholder="设置排程深度"
                />
              </el-form-item>
              <el-form-item label="优化目标">
                <el-select
                  v-model="algorithmParams.optimizationTarget"
                  placeholder="选择优化目标"
                >
                  <el-option
                    label="最小化完工时间"
                    value="minimize_completion_time"
                  />
                  <el-option
                    label="最大化设备利用率"
                    value="maximize_utilization"
                  />
                  <el-option label="最小化延迟时间" value="minimize_delay" />
                  <el-option label="均衡负载" value="balance_load" />
                </el-select>
              </el-form-item>
              <el-form-item label="约束权重">
                <el-slider
                  v-model="algorithmParams.constraintWeight"
                  :min="0"
                  :max="100"
                  show-input
                />
              </el-form-item>
              <el-form-item label="迭代次数">
                <el-input-number
                  v-model="algorithmParams.iterations"
                  :min="10"
                  :max="10000"
                  :step="10"
                  placeholder="算法迭代次数"
                />
              </el-form-item>
              <el-form-item label="收敛阈值">
                <el-input-number
                  v-model="algorithmParams.convergenceThreshold"
                  :min="0.001"
                  :max="1"
                  :step="0.001"
                  :precision="3"
                  placeholder="收敛阈值"
                />
              </el-form-item>
              <el-form-item label="启用并行计算">
                <el-checkbox v-model="algorithmParams.enableParallel" />
              </el-form-item>
              <el-form-item label="线程数量">
                <el-input-number
                  v-model="algorithmParams.threadCount"
                  :min="1"
                  :max="16"
                  :disabled="!algorithmParams.enableParallel"
                />
              </el-form-item>
            </el-form>
          </el-row>
        </div>
      </transition>
    </div>

    <!-- AI模型配置 -->
    <div class="aps-section">
      <el-row>
        <span
          style="
            font-weight: 600;
            margin-top: 1px;
            font-size: large;
            color: rgb(1, 128, 165);
          "
        >
          AI模型配置
        </span>
        <el-button type="text" style="float: right" @click="toggleAIParams">
          {{ showAIParams ? "收起" : "展开" }}
        </el-button>
      </el-row>
      <transition name="slide-toggle">
        <div v-show="showAIParams">
          <el-row>
            <el-form
              label-position="right"
              label-width="140px"
              style="margin-top: 30px"
            >
              <el-form-item label="模型类型">
                <el-select
                  v-model="aiParams.modelType"
                  placeholder="选择AI模型类型"
                >
                  <el-option label="深度强化学习" value="deep_rl" />
                  <el-option label="遗传算法" value="genetic_algorithm" />
                  <el-option label="神经网络" value="neural_network" />
                  <el-option label="混合算法" value="hybrid" />
                </el-select>
              </el-form-item>
              <el-form-item label="学习率">
                <el-input-number
                  v-model="aiParams.learningRate"
                  :min="0.0001"
                  :max="1"
                  :step="0.0001"
                  :precision="4"
                  placeholder="模型学习率"
                />
              </el-form-item>
              <el-form-item label="训练轮数">
                <el-input-number
                  v-model="aiParams.epochs"
                  :min="1"
                  :max="1000"
                  placeholder="训练轮数"
                />
              </el-form-item>
              <el-form-item label="批处理大小">
                <el-input-number
                  v-model="aiParams.batchSize"
                  :min="1"
                  :max="512"
                  placeholder="批处理大小"
                />
              </el-form-item>
              <el-form-item label="模型路径">
                <el-input
                  v-model="aiParams.modelPath"
                  placeholder="AI模型文件路径"
                  style="width: 80%; margin-right: 10px"
                />
                <el-button
                  type="primary"
                  size="small"
                  class="idoo-details-button"
                >
                  <el-icon><FolderOpened /></el-icon>
                  浏览
                </el-button>
              </el-form-item>
              <el-form-item label="启用GPU加速">
                <el-checkbox v-model="aiParams.enableGPU" />
              </el-form-item>
              <el-form-item label="GPU设备ID">
                <el-input-number
                  v-model="aiParams.gpuDeviceId"
                  :min="0"
                  :max="7"
                  :disabled="!aiParams.enableGPU"
                />
              </el-form-item>
              <el-form-item label="模型版本">
                <el-input
                  v-model="aiParams.modelVersion"
                  placeholder="如: v1.0.0"
                />
              </el-form-item>
            </el-form>
          </el-row>
        </div>
      </transition>
    </div>

    <!-- 高级设置 -->
    <div class="aps-section">
      <el-row>
        <span
          style="
            font-weight: 600;
            margin-top: 1px;
            font-size: large;
            color: rgb(1, 128, 165);
          "
        >
          高级设置
        </span>
        <el-button
          type="text"
          style="float: right"
          @click="toggleAdvancedParams"
        >
          {{ showAdvancedParams ? "收起" : "展开" }}
        </el-button>
      </el-row>
      <transition name="slide-toggle">
        <div v-show="showAdvancedParams">
          <el-row>
            <el-form
              label-position="right"
              label-width="140px"
              style="margin-top: 30px"
            >
              <el-form-item label="调试模式">
                <el-checkbox v-model="advancedParams.debugMode" />
              </el-form-item>
              <el-form-item label="日志级别">
                <el-select
                  v-model="advancedParams.logLevel"
                  placeholder="选择日志级别"
                >
                  <el-option label="DEBUG" value="debug" />
                  <el-option label="INFO" value="info" />
                  <el-option label="WARNING" value="warning" />
                  <el-option label="ERROR" value="error" />
                </el-select>
              </el-form-item>
              <el-form-item label="性能监控">
                <el-checkbox v-model="advancedParams.performanceMonitoring" />
              </el-form-item>
              <el-form-item label="内存限制(GB)">
                <el-input-number
                  v-model="advancedParams.memoryLimit"
                  :min="1"
                  :max="64"
                  placeholder="内存使用限制"
                />
              </el-form-item>
              <el-form-item label="超时时间(秒)">
                <el-input-number
                  v-model="advancedParams.timeout"
                  :min="30"
                  :max="3600"
                  placeholder="操作超时时间"
                />
              </el-form-item>
            </el-form>
          </el-row>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from "vue";
import { FolderOpened } from "@element-plus/icons-vue";
import BaseParametersConfig from "@/components/visualiszation/BaseParametersConfig.vue";

export default {
  name: "SystemConfiguration",
  components: {
    FolderOpened,
    BaseParametersConfig,
  },
  setup() {
    // 组件引用
    const baseParamsRef = ref(null);

    // 显示控制
    const showAlgorithmParams = ref(true);
    const showAIParams = ref(true);
    const showAdvancedParams = ref(false);

    // 基础参数
    const baseParams = ref({
      startTime: null,
      endTime: null,
      selectedAlgorithm: null,
      useOvertime: false,
      balanceCapacity: false,
    });

    // 算法参数
    const algorithmParams = reactive({
      schedulingDepth: 10,
      optimizationTarget: "minimize_completion_time",
      constraintWeight: 50,
      iterations: 100,
      convergenceThreshold: 0.001,
      enableParallel: true,
      threadCount: 4,
    });

    // AI参数
    const aiParams = reactive({
      modelType: "deep_rl",
      learningRate: 0.001,
      epochs: 100,
      batchSize: 32,
      modelPath: "",
      enableGPU: false,
      gpuDeviceId: 0,
      modelVersion: "v1.0.0",
    });

    // 高级参数
    const advancedParams = reactive({
      debugMode: false,
      logLevel: "info",
      performanceMonitoring: true,
      memoryLimit: 8,
      timeout: 300,
    });

    // 切换显示状态
    const toggleAlgorithmParams = () => {
      showAlgorithmParams.value = !showAlgorithmParams.value;
    };

    const toggleAIParams = () => {
      showAIParams.value = !showAIParams.value;
    };

    const toggleAdvancedParams = () => {
      showAdvancedParams.value = !showAdvancedParams.value;
    };

    // 获取配置数据
    const getConfigData = () => {
      return {
        baseParameters: baseParamsRef.value?.getFormData() || baseParams.value,
        algorithmParameters: { ...algorithmParams },
        aiParameters: { ...aiParams },
        advancedParameters: { ...advancedParams },
      };
    };

    // 加载配置数据
    const loadConfigData = (data) => {
      if (data.baseParameters) {
        baseParams.value = { ...baseParams.value, ...data.baseParameters };
        if (baseParamsRef.value) {
          baseParamsRef.value.loadData(data.baseParameters);
        }
      }
      if (data.algorithmParameters) {
        Object.assign(algorithmParams, data.algorithmParameters);
      }
      if (data.aiParameters) {
        Object.assign(aiParams, data.aiParameters);
      }
      if (data.advancedParameters) {
        Object.assign(advancedParams, data.advancedParameters);
      }
    };

    // 验证配置
    const validateConfig = () => {
      const errors = [];

      // 验证基础参数
      if (baseParamsRef.value) {
        const baseValidation = baseParamsRef.value.validateForm();
        if (!baseValidation.valid) {
          errors.push(...baseValidation.errors);
        }
      }

      // 验证算法参数
      if (algorithmParams.schedulingDepth < 1) {
        errors.push("排程深度必须大于0");
      }
      if (!algorithmParams.optimizationTarget) {
        errors.push("请选择优化目标");
      }
      if (algorithmParams.iterations < 10) {
        errors.push("迭代次数不能少于10");
      }

      // 验证AI参数
      if (!aiParams.modelType) {
        errors.push("请选择AI模型类型");
      }
      if (aiParams.learningRate <= 0) {
        errors.push("学习率必须大于0");
      }
      if (aiParams.epochs < 1) {
        errors.push("训练轮数必须大于0");
      }

      return {
        valid: errors.length === 0,
        errors,
      };
    };

    // 重置配置
    const resetConfig = () => {
      // 重置基础参数
      if (baseParamsRef.value) {
        baseParamsRef.value.resetForm();
      }

      // 重置算法参数
      Object.assign(algorithmParams, {
        schedulingDepth: 10,
        optimizationTarget: "minimize_completion_time",
        constraintWeight: 50,
        iterations: 100,
        convergenceThreshold: 0.001,
        enableParallel: true,
        threadCount: 4,
      });

      // 重置AI参数
      Object.assign(aiParams, {
        modelType: "deep_rl",
        learningRate: 0.001,
        epochs: 100,
        batchSize: 32,
        modelPath: "",
        enableGPU: false,
        gpuDeviceId: 0,
        modelVersion: "v1.0.0",
      });

      // 重置高级参数
      Object.assign(advancedParams, {
        debugMode: false,
        logLevel: "info",
        performanceMonitoring: true,
        memoryLimit: 8,
        timeout: 300,
      });
    };

    return {
      baseParamsRef,
      baseParams,
      algorithmParams,
      aiParams,
      advancedParams,
      showAlgorithmParams,
      showAIParams,
      showAdvancedParams,
      toggleAlgorithmParams,
      toggleAIParams,
      toggleAdvancedParams,
      getConfigData,
      loadConfigData,
      validateConfig,
      resetConfig,
    };
  },
};
</script>

<style scoped>
.system-configuration {
  padding: 0;
}

.section-header {
  margin-bottom: 30px;
  text-align: center;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: #714b67;
  margin-bottom: 10px;
  font-family: "Microsoft YaHei", "Noto Sans SC", sans-serif;
}

.section-description {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.aps-section {
  background-color: #ebebeb;
  padding: 15px;
  margin-top: 20px;
  border-radius: 5px;
}

.slide-toggle-enter-active,
.slide-toggle-leave-active {
  transition: all 0.3s ease;
  overflow: hidden;
}

.slide-toggle-enter,
.slide-toggle-leave-to {
  max-height: 0;
  opacity: 0;
}

.slide-toggle-enter {
  max-height: 0;
}

.slide-toggle-leave-to {
  max-height: 0;
}

:deep(.el-form-item__label) {
  color: #714b67 !important;
  font-weight: 600;
}

:deep(.el-input-number) {
  width: 100%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-slider) {
  margin-left: 12px;
  margin-right: 12px;
}
</style>
